<?php
class ProductlogAction extends CommonAction {

	public function _before_add() {
		$Role = M('Role');
		$role_sale = $Role->getByName('销售顾问'); // 获取销售顾问
		$role_id = $role_sale[id];
		$Saler = M('User');
		$salers = $Saler->join('dudu_role_user on id= dudu_role_user.user_id')->where("dudu_role_user.role_id=$role_id")->select();
		$this->assign("salers", $salers);

		$Product = M('Product'); //产品列表
		$products = $Product->where('status=1')->order('typeId desc')->select();
		$this->assign('products', $products, 1);
		$Card = M('Card');
		$card = $Card->getById($_REQUEST['cardId']);
		$this->assign('card', $card);
	}


	public function insert() { //添加产品购买记录

		$errorType ='事务未能完成';
		$Productlog = M('Productlog');
		$Productlog->startTrans();
		$data = array ();
		$data['memberId'] = $_REQUEST['memberId'];
		$data['salerId'] = $_REQUEST['salerId'];
		$data['productId'] = $_REQUEST['productId'];
		$data['useTimes'] = $_REQUEST['useTimes'];
		$data['addTime'] = time();
		$Product = M('Product');
		$product = $Product->getById($_REQUEST['productId']);
		$data['productName'] = $product['name'];
		$plid = $Productlog->add($data);

		if ($plid) { //添加消费记录
			$log['memberId'] = $_REQUEST['memberId'];
			$log['cardId'] = $_REQUEST['cardId'];
			$log['money'] = $_REQUEST['price'];
			$log['payTime'] = time();
			$log['payType'] = 1; //0:预存,1:消费,-1,退款
			$log['salerId'] = $_REQUEST['salerId'];
			$log['operatorId'] = $_SESSION[C('USER_AUTH_KEY')]; //操作员id
			$log['remark'] = $_REQUEST['productName'];
			$Paylog = M('Paylog');

			$payLogId = $Paylog->add($log);
			if ($payLogId) { //更新会员卡信息 扣除余额
				$Card = M('Card');
				$cardInfo = $Card->getById($_REQUEST['cardId']);
				if ($_REQUEST['price'] > 0) { //非免费产品

					if ($_REQUEST['isExt'] == 1) { // 使用优惠金额
						$enough = $cardInfo['extMoney'] + $cardInfo['money'] - $_REQUEST['price']; //计算余额是否足够
						if ($enough < 0) {
							$r = false; //余额不足
							$errorType = "余额不足";
						} else {
							$isExtEnough = $cardInfo['extMoney'] - $_REQUEST['price'];
							if ($isExtEnough > 0) { //优惠金额足够
								$r = $Card->setDec('extMoney', "id={$_REQUEST['cardId']}", $_REQUEST['price']);
							} else { //优惠金额不足够
								$payMoney = $_REQUEST['price'] - $cardInfo['extMoney'];
								$cardData['money'] = $cardInfo['money'] - $payMoney;
								$cardData['extMoney'] = 0;
								$r = $Card->where("id={$_REQUEST['cardId']}")->save($cardData);
							}
						}

					} else {// 不使用优惠金额
						$enough = $cardInfo['money'] - $_REQUEST['price']; //计算余额是否足够
						if ($enough < 0) {
							$r = false; //余额不足
							$errorType = "余额不足";
						} else {
							$r = $Card->setDec('money', "id={$_REQUEST['cardId']}", $_REQUEST['price']);
						}
					}
				} else {
					$r = true; //免费产品
				}
				if ($r) {
					$productlogData['payId'] = $payLogId;
					$Productlog->where("id=$plid")->save($productlogData); //添加产品购买记录的 付款id
					$Productlog->commit(); //事务提交

					$this->assign('jumpUrl', '__APP__/Member/showInfo/id/' . $_REQUEST['memberId'] . '/fff');
					$this->success('购买成功!');
					return;
				}
			}
			//失败提示
			$Productlog->rollback(); //事务回滚
			$this->error($errorType.'购买失败!');
		}
	}
}
?>